//
//  DropMenuView.m
//  BP_NewClient
//
//  Created by  on 15/3/2.
//  Copyright (c) 2015年 normal. All rights reserved.
//

#import "DropMenuView.h"

@interface DropMenuView ()<JSDropDownMenuDataSource,JSDropDownMenuDelegate>{
    
    NSMutableArray *_data1;
    NSMutableArray *_data2;
    NSMutableArray *_data3;
    
    NSInteger _currentData1Index;
    NSInteger _currentData2Index;
    NSInteger _currentData3Index;
    
    JSDropDownMenu *menu;
}

@property (nonatomic, strong) DropMenuBlock dropBlock;
@property (nonatomic, assign) NSInteger myCulom;

@end

@implementation DropMenuView

- (id)initWithFrame:(CGRect)rect WithData:(NSMutableArray *)data DropMenuBlock:(DropMenuBlock)dropBlock1{

    self = [super initWithFrame:rect];
    if (self) {
        self.dropBlock = dropBlock1;
        
        [self initData:data];
        
        menu = [[JSDropDownMenu alloc] initWithOrigin:CGPointMake(0, 0) andHeight:45];
        menu.indicatorColor = [UIColor colorWithRed:175.0f/255.0f green:175.0f/255.0f blue:175.0f/255.0f alpha:1.0];
        menu.separatorColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0];
        menu.textColor = [UIColor colorWithRed:83.f/255.0f green:83.f/255.0f blue:83.f/255.0f alpha:1.0f];
        menu.dataSource = self;
        menu.delegate = self;
        
        [self addSubview:menu];
    }
    return self;
}


- (void)initData:(NSMutableArray *)array{
    
    NSArray *leftZoneList = @[@"人民大学", @"苏州街", @"万柳", @"颐和园", @"香山植物园"];
    NSArray *rightZoneList = @[@"四惠", @"四惠东", @"太阳宫", @"国甜水园", @"北京东站"];
    
    _data1 = [NSMutableArray arrayWithObjects:@{@"title":@"朝阳区", @"data":leftZoneList}, @{@"title":@"海淀区", @"data":rightZoneList},@{@"title":@"东城区", @"data":leftZoneList}, @{@"title":@"西城区", @"data":rightZoneList},@{@"title":@"昌平区", @"data":leftZoneList}, @{@"title":@"大兴区", @"data":rightZoneList},@{@"title":@"顺义区", @"data":leftZoneList}, @{@"title":@"房山区", @"data":rightZoneList},@{@"title":@"通州区", @"data":leftZoneList}, @{@"title":@"亦庄区", @"data":rightZoneList}, nil];
    
    _data2 = [NSMutableArray arrayWithObjects:@"智能排序", @"离我最近", @"评价最高", @"最新发布", @"人气最高", @"价格最低", @"价格最高", nil];
    
    _data3 = [NSMutableArray arrayWithObjects:@"不限", @"美发",@"美甲",@"美容",@"SPA", @"健身", nil];
}

- (void)reloadData{
    [menu reloadData];
}

- (NSInteger)numberOfColumnsInMenu:(JSDropDownMenu *)menu {
    return 3;
}

-(BOOL)displayByCollectionViewInColumn:(NSInteger)column{
    // modify by chentaoli
    /*
    if (column==2) {
        
        return YES;
    }
    */
    BOOL isShow = menu.show;
    if (self.myCulom != column) {
        isShow = NO;
    }
    self.myCulom = column;

    NSLog(@"%d",menu.show);
    self.dropBlock(@"name",isShow);
    return NO;
}

-(BOOL)haveRightTableViewInColumn:(NSInteger)column{
    
    if (column==1) {
        return YES;
    }
    return NO;
}

-(CGFloat)widthRatioOfLeftColumn:(NSInteger)column{
    
    if (column==1) {
        return 0.3;
    }
    
    return 1;
}

-(NSInteger)currentLeftSelectedRow:(NSInteger)column{

    if (column==0) {
        return _currentData2Index;
    }
    if (column==1) {
        return _currentData1Index;
    }
    
    return 0;
}

- (NSInteger)menu:(JSDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow{
    
    if (column==0) {
        return _data3.count;

    } else if (column==1){
        
        if (leftOrRight==0) {
            return _data1.count;
        } else{
            NSDictionary *menuDic = [_data1 objectAtIndex:leftRow];
            return [[menuDic objectForKey:@"data"] count];
        }
    } else if (column==2){
        return _data2.count;
    }
    
    return 0;
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForColumn:(NSInteger)column{
    
    switch (column) {
        case 0:
            return _data3[0];
            break;
        case 1:
            return [[_data1[0] objectForKey:@"data"] objectAtIndex:0];

            break;
        case 2:
            return _data2[0];
            break;
        default:
            return nil;
            break;
    }
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column==0) {
        return _data3[indexPath.row];

        
    } else if (indexPath.column==1) {
        
        if (indexPath.leftOrRight==0) {
            NSDictionary *menuDic = [_data1 objectAtIndex:indexPath.row];
            return [menuDic objectForKey:@"title"];
        } else{
            NSInteger leftRow = indexPath.leftRow;
            NSDictionary *menuDic = [_data1 objectAtIndex:leftRow];
            return [[menuDic objectForKey:@"data"] objectAtIndex:indexPath.row];
        }
    } else {
        return _data2[indexPath.row];

    }
}

- (void)menu:(JSDropDownMenu *)menu didSelectRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column == 0) {
        _currentData3Index = indexPath.row;

    } else if(indexPath.column == 1){
        if(indexPath.leftOrRight==0){
            _currentData1Index = indexPath.row;
            return;
        }
    } else{
        _currentData2Index = indexPath.row;

    }
    
    if (self.dropBlock) {
        self.dropBlock(@"name",YES);
    }
    
}


@end
